<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/res/layui/css/layui.css" media="all">
    <script src="/res/js/jquery-3.4.1.min.js"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>


<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>注册</legend>
</fieldset>

<form class="layui-form" action="" lay-filter="example" id="myForm">

    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="userName" id="userName" lay-verify="title" autocomplete="off" placeholder="请输入账号"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="password" name="userPassword" id="userPassword" placeholder="请输入密码" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input type="email" name="userEmail" id="userEmail" placeholder="请输入邮箱" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">头像</label>
        <div class="layui-input-block">
            <input type="file" name="userPhoto_" id="userPhoto_" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">生日</label>
        <div class="layui-input-block">
            <input type="text" name="userBirthday_" id="userBirthday_" lay-verify="date" placeholder="yyyy-MM-dd"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input type="text" name="userPhone" id="userPhone" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">昵称</label>
        <div class="layui-input-block">
            <input type="text" name="userNickname" id="userNickname" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit="" lay-filter="tijiao" id="button-submit">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script src="/res/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#userBirthday_'
        });
        form.on('submit(tijiao)', function (data) {
            // layer.alert(JSON.stringify(data.field), {
            //     title: '最终的提交信息'
            // })

            //利用ajax向后台提交数据
            let url = "/logAndReg/reg.action";
            let formData = new FormData(document.getElementById("myForm"));
            console.info(formData);//输出表单数据
            $.ajax({
                type: 'POST',   //上传文件必须使用post
                url: url,
                data: formData, //需要传输的数据
                dataType: 'json',
                processData: false, // 告诉jquery要传输data对象
                contentType: false,   // 告诉jquery不需要增加请求头对于contentType的设置
                success: function (resp) { //如果上传成功,就执行
                    if (resp.state == 1) {
                        //注意：parent 是 JS 自带的全局对象，可用于操作父页面
                        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        parent.layer.close(index);
                        //父页面刷新
                        parent.table.reload('dataListTable');
                        //父页面提示信息
                        parent.layer.msg(resp.msg);
                    } else {
                        layer.msg(resp.msg);
                    }

                }
                , error: function (arg) { //如果上传失败,就执行
                    console.log(arg)
                }
            });

            return false;
        });

    });
</script>
</body>
</html>